import request, { _pureAxios } from './request.js'
import { handleRequest } from './helper.js'
import qs from 'qs'

// 获取楼层
export function getFloorsList(query) {
  return request({
    url: `/${query.modelId}/storeys`,
    method: 'get',
    params: query
  })
}

// 获取构件
export function getDominsList(query) {
  return request({
    url: `/${query.modelId}/components`,
    method: 'get',
    params: query
  })
}
// 查询构件
export function getComponentInfo(query, config) {
  const p = request({
    url: `/${query.modelId}/componentsBy`,
    method: 'get',
    params: query,
    paramsSerializer: params => {
      return qs.stringify(params, { indices: false })
    },
    ...config
  })
  return handleRequest(p)
}

// 批量查询构件
export function getComponentInfoBatch(data, config) {
  const p = request({
    url: `/componentsBy`,
    method: 'post',
    urlType: 'common',
    data,
    ...config
  })
  return handleRequest(p)
}

// 获取token
export function getToken(data, config) {
  const p = request({
    url: `/token`,
    method: 'post',
    data,
    ...config,
    urlType: 'common',
    noRepeat: true
  })
  return handleRequest(p)
}

// 获取历史（版本列表）
export function getHistory(query, config) {
  const p = request({
    url: `/${query.modelId}`,
    method: 'get',
    params: {
      history: true
    },
    ...config
  })
  return handleRequest(p)
}

// 获取所有模型
export function getModelList(query, config) {
  const p = request({
    url: '',
    method: 'get',
    params: query,
    ...config
  })
  return handleRequest(p)
}

// 上传模型
export function uploadModel(data, config) {
  const p = request({
    url: '',
    method: 'post',
    data,
    ...config
  })
  return handleRequest(p);
}

// 更新模型
export function updateModel(query, data, config) {
  const p = request({
    url: '/' + query.modelId,
    method: 'put',
    data,
    ...config
  })
  return handleRequest(p);
}

// 删除模型(此处是第三方平台的接口，不用改成post请求)
export function deleteModel(query, config) {
  const p = _pureAxios({
    url: '/' + query.modelId,
    method: 'delete',
    ...config
  })
  return handleRequest(p);
}

/**
 * 下载模型
 * @param {*} query 
 * @param {*} config 
 * @returns 
 */
export function downloadModel(query, config) {
  const p = request({
    url: `/${query.modelId}/download`,
    method: 'get',
    ...config,
    responseType: 'arraybuffer',
    unValidateCode: true,
  })
  return handleRequest(p);
}

// 查询所有构件
export function getAllComponents(query, config) {
  const p = request({
    url: `/${query.modelId}/components`,
    method: 'get',
    params: query,
    ...config
  })
  return handleRequest(p)
}